package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.MessageMapper;
import cn.group.hotel2.mapper.ReplyMapper;
import cn.group.hotel2.pojo.dto.ReplyAddNewDTO;
import cn.group.hotel2.pojo.entity.Reply;
import cn.group.hotel2.pojo.vo.MessageStandardVO;
import cn.group.hotel2.service.IReplyService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wcl
 * @version 0.0.1
 * @date 2022/12/1
 */
@Slf4j
@Service
public class ReplyServiceImpl implements IReplyService {

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private MessageMapper messageMapper;

    public ReplyServiceImpl() {
        log.debug("创建业务类:ReplyServiceImpl");
    }

    @Override
    public void addNew(ReplyAddNewDTO replyAddNewDTO) {
        log.debug("开始处理【添加回复】的业务,参数:{}", replyAddNewDTO);

        Long messageId = replyAddNewDTO.getMessageId();
        MessageStandardVO queryResult = messageMapper.getStandardById(messageId);
        if (queryResult == null) {
            String message = "添加回复失败,该条留言不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Reply reply = new Reply();
        BeanUtils.copyProperties(replyAddNewDTO, reply);
        log.debug("即将向回复表中添加数据,结果:{}", reply);
        int rows = replyMapper.insert(reply);
        if(rows != 1) {
            String message = "添加回复失败,系统繁忙,请稍后再次尝试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("添加回复完成!");
    }
}
